/*
 * @Description: 商品模块控制器
 */
const productCtrl = require("../model/controller/productCtrl");
module.exports = {
  /**
   * 获取商品分类
   */
  async GetCategory(req, res) {
    const category = await productCtrl.GetCategory();

    let result = {
      code: "001",
      category,
    };
    res.send(result);
  },
  /**
   * 根据商品分类名称获取首页展示的商品信息
   */
  async GetPromoProduct(req, res) {
    let { categoryName } = req.body;
    // 根据商品分类名称获取分类id
    const categoryID = await productCtrl.GetCategoryId(categoryName);
    // 根据商品分类id获取首页展示的商品信息
    const Product = await productCtrl.GetPromoProduct(categoryID);

    let result = {
      code: "001",
      Product,
    };
    res.send(result);
  },
  /**
   * 根据商品分类名称获取热门商品信息
   */
  async GetHotProduct(req,res) {
    let { categoryName } = req.body;

    // 根据商品分类名称获取分类id
    const category_id = await productCtrl.GetCategoryId(categoryName);

    // 根据商品分类id获取商品信息
    const Product = await productCtrl.GetProductByCategory(category_id, 0, 7);

    let result = {
      code: "001",
      Product,
    };
    res.send(result);
  },
  /**
   * 分页获取所有的商品信息
   */
   async GetAllProduct(req,res) {
    let { currentPage, pageSize } = req.body;
    // 计算开始索引
    const offset = (currentPage - 1) * pageSize;
    const Product = await productCtrl.GetAllProduct(offset, pageSize);
    // 获取所有的商品数量,用于前端分页计算
    const total = (await productCtrl.GetAllProduct()).length;
    let result = {
      code: "001",
      Product,
      total,
    };
    res.send(result);
  },
  /**
   * 根据分类id,分页获取商品信息
   */
  async GetProductByCategory(req,res) {
    let { categoryID, currentPage, pageSize } = req.body;
    // 计算开始索引
    const offset = (currentPage - 1) * pageSize;
    // 分页获取该分类的商品信息
    const Product = await productCtrl.GetProductByCategory(
      categoryID,
      offset,
      pageSize
    );
    // 获取该分类所有的商品数量,用于前端分页计算
    const total = (await productCtrl.GetProductByCategory(categoryID)).length;

    let result = {
      code: "001",
      Product,
      total,
    };
    res.send(result)
  },
  /**
   * 根据搜索条件,分页获取商品信息
   */
  async GetProductBySearch(req,res) {
    let { search, currentPage, pageSize } = req.body;
    // 计算开始索引
    const offset = (currentPage - 1) * pageSize;
    // 获取分类列表
    const category = await productCtrl.GetCategory();

    let Product;
    let total;

    for (let i = 0; i < category.length; i++) {
      // 如果搜索条件为某个分类名称,直接返回该分类的商品信息
      if (search == category[i].category_name) {
        // 获取该分类的商品信息
        Product = await productCtrl.GetProductByCategory(
          category[i].category_id,
          offset,
          pageSize
        );
        // 获取该分类所有的商品数量,用于前端分页计算
        total = (await productCtrl.GetProductByCategory(category[i].category_id))
          .length;

        let result = {
          code: "001",
          Product,
          total,
        };
        res.send(result);
        return;
      }
    }
    // 否则返回根据查询条件模糊查询的商品分页结果
    Product = await productCtrl.GetProductBySearch(search, offset, pageSize);
    // 获取模糊查询的商品结果总数
    total = (await productCtrl.GetProductBySearch(search)).length;

    let result = {
      code: "001",
      Product,
      total,
    };
    res.send(result);
  },
  /**
   * 根据商品id,获取商品详细信息
   */
  async GetDetails(req,res) {
    let { productID } = req.body;
    // console.log("okk");
    const Product = await productCtrl.GetProductById(productID);

    let result = {
      code: "001",
      Product,
    };
    res.send(result);
  },
  /**
   * 根据商品id,获取商品图片,用于食品详情的页面展示
   */
  async GetDetailsPicture(req,res) {
    let { productID } = req.body;
    const ProductPicture = await productCtrl.GetDetailsPicture(productID);

    let result = {
      code: "001",
      ProductPicture,
    };
    res.send(result);
  },
};
